package com.tencent.FaceTrackFaceu;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;

import android.view.SurfaceView;
import android.util.AttributeSet;

import com.tencent.youtufacetrack.YoutuFaceTrack;

public class DrawView extends SurfaceView
{
    private Paint p = new Paint();

    private float points[] = null;
    private float scale = 1;

    public DrawView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

//         p.setFilterBitmap(true);
        p.setARGB(255, 0, 255, 0);
        p.setStrokeWidth(3);

        // This call is necessary, or else the
        // draw method will not be called.
        setWillNotDraw(false);
    }

    public static float[] addAll(float[] array1, float[] array2)
    {
        if (array1 == null) {
            return array2.clone();
        }
        else if (array2 == null) {
            return array1.clone();
        }
        float[] joinedArray = new float[array1.length + array2.length];
        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
        return joinedArray;
    }
    //for native call
   // public void setPoints(NativeInterface.FaceStatus[] facestatus, int w, int h)
    //for java call
    public void setPoints(YoutuFaceTrack.FaceStatus[] facestatus, int w, int h)
    {
        points = null;

        if (facestatus != null && facestatus.length!=0)
        {
            for (int i=0; i<facestatus.length; i++)
            {
                points = addAll(points, facestatus[i].xys);
            }

            if (scale == 1)
            {
                scale = getWidth() / (float)w;
                if (scale > getHeight() / (float)h)
                {
                scale = getHeight() / (float)h;
                }
            }
        }

        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        if (points != null)
        {
            canvas.scale(scale, scale);
            canvas.drawPoints(points, p);
        }
    }
}
